package com.self.serviceedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.self.serviceedu.entity.EduTeacher;
import com.self.serviceedu.mapper.EduTeacherMapper;
import com.self.serviceedu.query.TeacherQuery;
import com.self.serviceedu.service.EduTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author xiaozeng
 * @since 2020-08-20
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    @Autowired
    private EduTeacherMapper eduTeacherMapper;

    //讲师模糊查询+分页查询
    @Override
    public void pageQuery(Page<EduTeacher> page, TeacherQuery teacherQuery) {

        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("sort");

        if(teacherQuery == null){
            eduTeacherMapper.selectPage(page,wrapper);
            return ;
        }

        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();

        System.out.println(name);
        System.out.println(level);

        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }

        if (!StringUtils.isEmpty(level)) {
            wrapper.eq("level",level);
        }

        if(!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }

        if(!StringUtils.isEmpty(end)){
            wrapper.le("gmt_modified",end);
        }

        eduTeacherMapper.selectPage(page,wrapper);


    }

    //重写删除方法
    @Override
    public boolean removeById(Serializable id) {
        Integer result = eduTeacherMapper.deleteById(id);
        return  result != null && result > 0 ;
    }

    //1. 分页查询讲师
    @Override
    public Map<String, Object> getTeacherFrontList(Page<EduTeacher> teacherPage) {

        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        //把分页数据封装进teacherPage对象中
         baseMapper.selectPage(teacherPage, wrapper);

        List<EduTeacher> records = teacherPage.getRecords();//每页数据的List集合
        long current = teacherPage.getCurrent();//当前页
        long pages = teacherPage.getPages();//总页数
        long size = teacherPage.getSize();//每页记录数
        long total = teacherPage.getTotal();//总记录数
        boolean hasNext = teacherPage.hasNext();//下一页
        boolean hasPrevious = teacherPage.hasPrevious();//前一页

        Map<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }
}
